package com.qf.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.mybatisplus.aspect.druid.DataBase;
import com.qf.mybatisplus.bean.dto.UserDto;
import com.qf.mybatisplus.bean.entity.*;
import com.qf.mybatisplus.bean.po.UserPo;
import com.qf.mybatisplus.bean.vo.RegistVo;
import com.qf.mybatisplus.bean.vo.TestVo;
import com.qf.mybatisplus.bean.vo.UserBackVo;
import com.qf.mybatisplus.mapper.UserMapper;
import com.qf.mybatisplus.mapper.VJhmxMapper;
import com.qf.mybatisplus.resultBean.JsonResult;
import com.qf.mybatisplus.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mybatisplus.utils.PageUtils;
import com.qf.mybatisplus.utils.RedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-05-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private VJhmxMapper vJhmxMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        Page<User> limit = new Page<>(Long.parseLong(params.get("currentPage").toString()), Long.parseLong(params.get("pagesize").toString()));
        IPage<User> page = this.page(limit, new LambdaQueryWrapper<User>());


        return new PageUtils(page);
    }

    @Override
    public User getOne(String name) {

        User one = userMapper.getOne(name);
        return one;
    }

    @Override
    @DataBase("db2")
    public User getUser(String name) {
        User oneUser = userMapper.getOne(name);
        return oneUser;
    }

    @Override
    public int batchInsert(List<User> userList) {
        int i = userMapper.batchInsert(userList);

        return i;
    }

    @Override
    public JsonResult toRegist(RegistVo registVo) {

        String mesCode = registVo.getMesCode();

        String redisMesCode = (String) redisUtils.get("mesCode");

        System.err.println("短信验证码:" + redisMesCode);
        if (!mesCode.equalsIgnoreCase(redisMesCode)) {
            return JsonResult.failed("验证码错误");
        }

        User user = new User();
        user.setAge(registVo.getAge());
        user.setCreateTime(new Date());
        user.setDeleted(0);
        user.setEmail(registVo.getEmail());
        user.setName(registVo.getName());
        user.setPassword(registVo.getPassword());
        user.setSex(registVo.getSex());
        user.setPhone(registVo.getPhone());
        user.setPhone(registVo.getPhone());

        int insert = userMapper.insert(user);

        return JsonResult.resultOk(insert);
    }

    @Override
    @DataBase("db2")
    public JsonResult insertVjhmx(List<VJhmx> list) {

        for (VJhmx vJhmx : list) {
            int i = vJhmxMapper.insertVJhmx(vJhmx);
            System.err.println(i);
        }
        return null;
    }

    @Override
    public JsonResult insertVHdcallspecail(List<VHdcallspecail> list) {

        for (VHdcallspecail vHdcallspecail : list) {
            int i = userMapper.insertHt(vHdcallspecail);
        }

        return null;
    }

    @Override
    public List<Role> getRoleList(Integer userId) {

        return userMapper.getRoleList(userId);
    }

    @Override
    public List<Permission> getPermissionList(Integer userId) {

        return userMapper.getPermissionList(userId);
    }

    @Override
    public List<User> getUserList(TestVo testVo) {
        User user = new User();
        BeanUtils.copyProperties(testVo, user);

        List<User> userList = userMapper.selectList(null);

        return null;
    }

    @Override
    public JsonResult insertOneUser(UserDto userDto) {
        UserPo userPo = new UserPo();
        BeanUtils.copyProperties(userDto, userPo);
        userPo.setCreateTime(new Date());
        userPo.setVersion(0);
        int i = userMapper.insertUser(userPo);
        if (i > 0) {
            return JsonResult.success();
        } else {
            return JsonResult.failed("插入失败");
        }
    }

    @Override
    public UserBackVo getOneUserByUserId(Integer userId) {

        User oneUser = userMapper.getOneUser(userId);
        UserBackVo userBackVo = new UserBackVo();
        BeanUtils.copyProperties(oneUser, userBackVo);
        return userBackVo;
    }

    @Override
    public List<User> getUserListInfo(Integer pagesize, Integer currentPage) {
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("create_time");
//        List<User> userList = userMapper.selectList(queryWrapper);

        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getName,"小王");
        List<User> userList = userMapper.selectList(userLambdaQueryWrapper);

//        List<User> userList = userMapper.getUserListInfo();
        return userList;
    }
    @Cacheable(value = "userCache", key = "#user.name", unless = "#result == null or #result.size() == 0")
    @Override
    public List<User> testSaveCache(User user) {
        List<User> userListInfo = userMapper.getUserListInfo();
        return userListInfo;
    }

    @CacheEvict(value = "userCache", key = "#user.name", beforeInvocation = false, condition = "#user ne  null ")
    @Override
    public void testDeleteCache(User user) {
        User user1 = new User();
        user1.setName("小王");
        user1.setAge(18);
        userMapper.insert(user);
    }

    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        String s1 = "s1";
        String s2 = "";
        Integer i1 = 0;
        Integer i2 = null;
        Object o = userService.checkNull(i2);
        System.err.println(o);
    }

    public Object checkNull(Object o) {

        if (o == null) {
            return "";
        }

        return o;

    }
}
